package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"person/model/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *personrpc.UserUpdateRequest) (*personrpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var finduser mysql.Area
	mysql.DB.Debug().Where("id=?", in.Id).First(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("用户不存在")
	}
	if finduser.UserId != int(in.Userid) {
		return nil, fmt.Errorf("不是当前登录用户信息")
	}

	user := mysql.Area{
		Model: gorm.Model{ID: uint(in.Id)},
		Name:  in.Name,
		Sex:   in.Sex,
		Img:   in.Img,
		Tel:   in.Tel,
	}
	mysql.DB.Debug().Updates(&user)

	return &personrpc.UserUpdateResponse{Id: int64(user.ID)}, nil
}
